package com.mygame

import com.almasb.fxgl.dsl.FXGL
import com.almasb.fxgl.dsl.components.RechargeableIntComponent
import com.almasb.fxgl.entity.Entity
import com.almasb.fxgl.entity.SpawnData
import com.almasb.fxgl.entity.state.EntityState
import com.almasb.fxgl.entity.state.StateComponent
import com.mygame.components.ATTACK
import com.mygame.components.JUMP_ATTACK
import com.mygame.components.SKILL1
import javafx.scene.shape.Rectangle
import java.util.*
import kotlin.math.max
import kotlin.math.min

fun spawn(eType: EType, data: SpawnData): Entity =
    FXGL.getGameWorld().spawn(eType.name.lowercase(Locale.getDefault()), data)

fun RechargeableIntComponent.changePoint(point: Number = 0) {
    if (point is Int) {
        val x = value + point
        value = if (point < 0) {//扣血最小要是0
            max(0, x)
        } else {//补血最大要是血上限
            min(maxValue, x)
        }
    } else if (point is Double) {
        val x = (value * (1 + point)).toInt()
        value = if (point < 0.0) {//扣血最小要是0
            max(0, x)
        } else {//补血最大要是血上限
            min(maxValue, x)
        }
    }
    val hpMaxWidth = entity.properties.getDouble(Const.RADIUS)
    val hpBox = entity.viewComponent.getChild(1, Rectangle::class.java)
    hpBox.width = hpMaxWidth * value / maxValue
}

fun StateComponent.changeAttackState(state: EntityState): Boolean {
    if (this.currentState == ATTACK
        || this.currentState == JUMP_ATTACK
        || this.currentState == SKILL1
    ) {
        return false
    }
    this.changeState(state)
    return true
}